

namespace Enlinium.Context
{
    public interface ISparseSet<T1, T2>
    {
        /// <summary>
        /// 外部稀疏集，数值表示紧密集的索引，位置代表紧密集的值
        /// </summary>
        List<T2> Sparse { get; set; }
        /// <summary>
        /// 内部紧密集，数值代表稀疏集的索引，位置代表稀疏集的值
        /// </summary>
        List<T1> Packed { get; set; }
        int GetId(T1 obj)
        {
            if (obj is int num)
            {
                return num;
            }
            else if (obj is Entity.Entity entity)
            {
                return entity.Id;
            }
            else if (obj is Component.IComponent component)
            {
                return component.Id;
            }
            throw new NullReferenceException("GetId：传入对象不存在");
        }
        int GetId(T2 obj)
        {
            if (obj is int num)
            {
                return num;
            }
            else if (obj is Entity.Entity entity)
            {
                return entity.Id;
            }
            else if (obj is Component.IComponent component)
            {
                return component.Id;
            }
            throw new NullReferenceException("GetId：传入对象不存在");
        }
        void Add(T1 packed, T2 sparse);
        void RemoveFromPackedAt(int index);
        void RemoveFromSparseAt(int index);
    }
    public class SparseSet<T1, T2> : ISparseSet<T1, T2>
    {
        /// <summary>
        /// 外部稀疏集，数值表示紧密集的索引，位置代表紧密集的值
        /// </summary>
        List<T2> Sparse { get; set; } = [];
        /// <summary>
        /// 内部紧密集，数值代表稀疏集的索引，位置代表稀疏集的值
        /// </summary>
        List<T1> Packed { get; set; } = [];
        int GetId(T1 obj)
        {
            if (obj is int num)
            {
                return num;
            }
            else if (obj is Entity.Entity entity)
            {
                return entity.Id;
            }
            else if (obj is Component.IComponent component)
            {
                return component.Id;
            }
            throw new NullReferenceException("GetId：传入对象不存在");
        }
        int GetId(T2 obj)
        {
            if (obj is int num)
            {
                return num;
            }
            else if (obj is Entity.Entity entity)
            {
                return entity.Id;
            }
            else if (obj is Component.IComponent component)
            {
                return component.Id;
            }
            throw new NullReferenceException("GetId：传入对象不存在");
        }
        void Add(T1 packed, T2 sparse)
        {
            {
                Packed.Add(packed);
                Sparse.Insert(GetId(packed), sparse);
            }
        }
        void RemoveFromPackedAt(int index);
        void RemoveFromSparseAt(int index);
    }
}